package com.fresh.bus.chat.ctrl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

import com.eeplat.social.openapi.callback.GlobalConfig;
import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Role;
import com.fresh.bus.canoe.session.Session;

@Controller("userCtrl")
public class UserCtrl extends BaseCtrl {

	@CanoeCommand(value = OpCodes.UPGRADE_LEVEL)
	public Response upgrade(Session session, Request request) {
		@SuppressWarnings("unchecked")
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.UPGRADE_LEVEL);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (userService.upgrade(session, data)) {
				map.put("status", "true");
			} else {
				map.put("status", "false");
			}
			map.put("user", userService.findUser(id));
		} catch (Exception e) {
			log.error("Upgrade failed...", e);
			return null;
		}
		response.setData(map);
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.UPLOAD_PIC)
	public Response uploadPicture(Session session, Request request) {
		byte[] data = (byte[]) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.UPLOAD_PIC);
		try {
			// 指定文件存放路径将读取到客户提交的数据写入文件中
			String rootDir = GlobalConfig.getValue("photo.rootdir");
			File dir = new File(rootDir);
			File file = new File(dir, id + ".jpg");
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(data);
			fos.close();
			response.setData(true);
		} catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(false);
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.READ_USER)
	public Response readeUser(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = (String) data.get("u");
		Response response = new Response(OpCodes.READ_USER);
		try {
			response.setData(this.userService.readUser(id));
			return response;
		} catch (Exception e) {
			log.error("readeUser failed..." + id, e);
			this.reportError(session);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.IS_ONLINE)
	public Response isOnline(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String user = (String) data.get("u");
		Response response = new Response(OpCodes.IS_ONLINE);
		try {
			Session us = this.roleManager.getSession(user);
			boolean flag = this.loginService.sessions.contains(user);
			response.setData(us!=null && flag);
		} catch (Exception e) {
			log.error("isOnline failed..." + user, e.getMessage());
			response.setData(false);
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.MODIFY_USER)
	public Response modifyUser(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.MODIFY_USER);
		try {
			data.put("id", id);
			this.userService.updateUserInfo(data);
			this.userService.synchroUser(session);
			response.setData(true);
		} catch (Exception e) {
			log.error("Login failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.USER_REPORT)
	public Response evaluateUser(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		String to = (String) session.getAttribute("to");
		try {
			if (!data.isEmpty()) {
				userService.evaluteUser(data, to, id);
			}
			session.removeAttribute("to");
			return busCtrl.queryRoomInfo(session, request);
		} catch (Exception e) {
			log.error("Login failed...", e);
			return null;
		}
	}

	@CanoeCommand(value = OpCodes.USER_COMMENT)
	public Response commentUser(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		String to = (String) session.getAttribute("to");
		try {
			if (!data.isEmpty()) {
				userService.commentUser(data, to, id);
				expService.update(session, data, OpCodes.USER_COMMENT);
			}
			Response response = new Response(OpCodes.USER_COMMENT);
			// expService.update(session, data, OpCodes.USER_REPORT);
			response.setData("");
			return response;
		} catch (Exception e) {
			log.error("Login failed...", e);
			return null;
		}
	}

	@CanoeCommand(value = OpCodes.QUERY_DATE_MSG)
	public Response queryDateList(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = (String) session.getAttribute("to");
		try {
			Response response = new Response(OpCodes.QUERY_DATE_MSG);
			response.setData(userService.queryUserDates(id).toArray());
			return response;
		} catch (Exception e) {
			log.error("Query comment failed...", e);
			return null;
		}
	}

	@CanoeCommand(value = OpCodes.REQUEST_DATE)
	public Response requestDate(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		String to = (String) session.getAttribute("to");
		Response response = new Response(OpCodes.REQUEST_DATE);
		// expService.update(session, data, OpCodes.USER_REPORT);
		response.setData(data);
		if (session.getAttribute("to") != null) {
			List<Role> recievers = new ArrayList<Role>();
			// recievers.add(session.getRole());
			recievers.add(roleManager.getRoleById(to));
			response.setRecievers(recievers);
			return response;
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.CONFIRM_DATE)
	public Response confirmDate(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		String to = (String) session.getAttribute("to");
		try {
			if (!data.isEmpty()) {
				userService.commentUser(data, to, id);
				expService.update(session, data, OpCodes.USER_COMMENT);
			}
			Response response = new Response(OpCodes.USER_COMMENT);
			// expService.update(session, data, OpCodes.USER_REPORT);
			response.setData("");
			return response;
		} catch (Exception e) {
			log.error("Login failed...", e);
			return null;
		}
	}

	@CanoeCommand(value = OpCodes.QUERY_COMMENT)
	public Response queryComment(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = (String) session.getAttribute("to");
		try {
			Response response = new Response(OpCodes.QUERY_COMMENT);
			response.setData(userService.queryUserComments(id).toArray());
			return response;
		} catch (Exception e) {
			log.error("Query comment failed...", e);
			return null;
		}
	}

	@CanoeCommand(value = OpCodes.GUIDE_DONE)
	public Response guideDone(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		try {
			int indexPos = (Integer)data.get("indexPos");
			userService.forwardGuid(session,indexPos);
			return null;
		} catch (Exception e) {
			log.error("Query comment failed...", e);
			return null;
		}
	}

	@CanoeCommand(value = OpCodes.SHARE_REWARD)
	public Response getShareReward(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.SHARE_REWARD);
		try {
			userService.getShareReward(id);
			response.setData(true);
		} catch (Exception e) {
			log.error("GetShareReward failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}
}
